package com.glory.brepda.entity.response;

import android.content.Context;
import android.util.Log;


import com.glory.brepda.base.MyApp;
import com.glory.brepda.entity.Exception;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.io.Serializable;
import java.util.ArrayList;

import androidx.fragment.app.FragmentManager;

public class BaseResponseBean<T> implements Serializable {

    private ReturnBean<T> Response;

    private transient FreeUI_GeneralFragmentDialog tipDialog;

    public ReturnBean<T> getResponse() {
        return Response;
    }

    public void setResponse(ReturnBean<T> response) {
        this.Response = response;
    }

    //获取到对象后，必须要判断是否获取成功
    public boolean isSuccess(Context context, boolean isHaveBody, FragmentManager fragmentManager) {
        return isSuccess(context, isHaveBody, fragmentManager, true);
    }

    // 24/7/3 901713 重写isSuccess 方法，加了个标志可控制是否显示接口返回的错误信息
    // isShowResultErrorMessage 是否显示接口返回的错误信息
    public boolean isSuccess(Context context, boolean isHaveBody, FragmentManager fragmentManager, boolean isShowResultErrorMessage) {
        String ReturnCode = "";
        String ReturnMessage = "";
        if (Response.getReturn() != null) {
            ReturnCode = Response.getReturn().getReturnCode().toUpperCase();
            ReturnMessage = Response.getReturn().getReturnMessage();
        } else {
            ReturnCode = Response.getHeader().getRESULT().toUpperCase();
            ReturnMessage = getMessage(Response.getHeader());
        }
        if (ReturnCode.equals("SUCCESS")) {
            if (isHaveBody) {
                if (Response.getBody() == null || Response.getBody().equals("")) {
                    showFailDialog("暂无请求内容", fragmentManager);
                    return false;
                } else {
                    if (Response.getBody() instanceof ArrayList) {
                        if ((((ArrayList) Response.getBody()).size()) > 0) {
                            return true;
                        } else {
                            showFailDialog("暂无请求数据", fragmentManager);
                            return false;
                        }
                    } else {
                        return true;
                    }
                }
            } else {
                return true;
            }
        } else {
            // 24/7/3 901713 新增标志位，接口返回错误时可弹出或不弹出错误窗口
            if (isShowResultErrorMessage) {
                showFailDialog(ReturnMessage, fragmentManager);
            }
            return false;
        }
    }


     private void showFailDialog(String message, FragmentManager fragmentManager) {
        tipDialog = FreeApi_DialogUtils.getConfirmDialog(MyApp.app, "确定", message, () -> {
        });
        tipDialog.show(fragmentManager,"DIALOG");
    }

    private String getMessage(ResponseHeader resultCode){
        String message = "";
//        Exception e = new Exception();
//        String[] result_code_en = e.getResult_code_en();
//        String[] result_message = e.getResult_message_ch();
//        for (int i = 0; i < result_code_en.length; i++) {
////            if (resultCode.getRESULTCODE().contains("#")){
////                String[] resultCodes = resultCode.getRESULTCODE().split("#");
////                resultCode.setRESULTCODE(resultCodes[0]);
////            }
//            if (result_code_en[i].equals(resultCode.getRESULTCODE().equals("") ? resultCode.getRESULTMESSAGE() : resultCode.getRESULTCODE())){
//                message = result_message[i];
//                break;
//            }
//
//        }
        if (FreeApi_StringUtils.isEmpty(message)){
            if (resultCode.getRESULTMESSAGE().contains("MES TimeOut")){
                message = "MES超时，请重新请求!";
            }else {
                if (!FreeApi_StringUtils.isEmpty(resultCode.getRESULTMESSAGE())){
                    message = resultCode.getRESULTMESSAGE();
                }else {
                    message = resultCode.getRESULTCODE();
                }

            }
        }

        return message.equals("") ? "服务器返回失败!" : message;
    }

}
